package com.bit.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/response")
public class ResponseController {

    @RequestMapping("/returnPage")
    public String returnPage(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml(){
        return "<h1>我是一级标题</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public List<Person> returnJson(){
        List<Person> people = new ArrayList<>();
        Person person = new Person("zhangsan", 21);
        people.add(person);
        return people;
    }

    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String, String> returnMap(){
        Map<String, String> map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(500);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping("/setSHeader")
    public String setHeader(HttpServletResponse response){
        response.setHeader("myHeader","myHeader");
        return "设置Header成功";
    }

    @ResponseBody
    @RequestMapping( "/setContentType")
    public String setContentType(HttpServletResponse response){
        return "{\"ok\": 1}";
    }
}
